package com.netty.exmple.zerocopy;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class NewIOServer {
    public static void main(String[] args) throws Exception {
        InetSocketAddress address = new InetSocketAddress(8888);
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(true);
        ServerSocket serverSocket = serverSocketChannel.socket();
        serverSocket.setReuseAddress(true);
        serverSocket.bind(address);
        ByteBuffer byteBuffer = ByteBuffer.allocate(4096);
        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept();
            socketChannel.configureBlocking(true);
            long readCount = 0L;
            long totalCount = 0L;
            while (readCount != -1) {
                try {
                    readCount = socketChannel.read(byteBuffer);
                    totalCount += readCount;
                } catch (Exception e) {
                    break;
                }
                byteBuffer.rewind();
            }
            System.out.println("发送的字节数：" + totalCount);
        }
    }
}
